package com.google.code.sms.dao;

import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.*;

import com.google.code.sms.Logger;

/**
 * Abstract superclass for all DAO's.
 *
 * @author Sindre Mehus
 */
public class AbstractDao {

    private static final Logger LOG = Logger.getLogger("AbstractDao");
    private DaoHelper daoHelper;

    /**
     * Returns a JDBC template for performing database operations.
     *
     * @return A JDBC template.
     */
    public JdbcTemplate getJdbcTemplate() {
        return daoHelper.getJdbcTemplate();
    }

    protected String questionMarks(String columns) {
        int count = columns.split(", ").length;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < count; i++) {
            builder.append('?');
            if (i < count - 1) {
                builder.append(", ");
            }
        }
        return builder.toString();
    }

    protected String prefix(String columns, String prefix) {
        StringBuilder builder = new StringBuilder();
        for (String s : columns.split(", ")) {
            builder.append(prefix).append(".").append(s).append(",");
        }
        if (builder.length() > 0) {
            builder.setLength(builder.length() - 1);
        }
        return builder.toString();
    }

    protected int update(String sql, Object... args) {
        int result = getJdbcTemplate().update(sql, args);
        return result;
    }

    protected <T> List<T> query(String sql, RowMapper rowMapper, Object... args) {
        List<T> result = getJdbcTemplate().query(sql, args, rowMapper);
        return result;
    }

    protected List<String> queryForStrings(String sql, Object... args) {
        List<String> result = getJdbcTemplate().queryForList(sql, args, String.class);
        return result;
    }

    protected Integer queryForInt(String sql, Integer defaultValue, Object... args) {
        List<Integer> list = getJdbcTemplate().queryForList(sql, args, Integer.class);
        Integer result = list.isEmpty() ? defaultValue : list.get(0) == null ? defaultValue : list.get(0);
        return result;
    }

    protected Date queryForDate(String sql, Date defaultValue, Object... args) {
        long t = System.nanoTime();
        List<Date> list = getJdbcTemplate().queryForList(sql, args, Date.class);
        Date result = list.isEmpty() ? defaultValue : list.get(0) == null ? defaultValue : list.get(0);
        return result;
    }

    protected Long queryForLong(String sql, Long defaultValue, Object... args) {
        List<Long> list = getJdbcTemplate().queryForList(sql, args, Long.class);
        Long result = list.isEmpty() ? defaultValue : list.get(0) == null ? defaultValue : list.get(0);
        return result;
    }

    protected <T> T queryOne(String sql, RowMapper rowMapper, Object... args) {
        List<T> list = query(sql, rowMapper, args);
        return list.isEmpty() ? null : list.get(0);
    }

    public void setDaoHelper(DaoHelper daoHelper) {
        this.daoHelper = daoHelper;
    }
}
